﻿using Liju.Model;
using System.Collections.Generic;
using System.Data;
namespace LiJu.Db
{
    public class CurveDbHelper
    {
        /// <summary>
        /// 添加曲线数据
        /// </summary>
        /// <returns></returns>
        public static bool AddCurve(CurveModel curveModel)
        {
            string strMySql = $"INSERT INTO curve (TaskId, Torques, Angles,TimeSpan) VALUES({curveModel.TaskId}, '{curveModel.Torques}', '{curveModel.Angles}', {curveModel.TimeSpan}); ";
            int result = DalMySQL.ExecuteNonQuery(strMySql);
            return result > 0;
        }


        /// <summary>
        /// 按照TaskId查询曲线
        /// </summary>
        /// <returns></returns>
        public static CurveModel GetCurveByTaskID(long TaskID)
        {
            string strMySql = $"select * from curve where TaskId={TaskID} ";
            DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
            var listCurve = DbExtension.DataTableToList<CurveModel>(dt);
            return listCurve.Count > 0 ? listCurve[0] : null;
        }

        /// <summary>
        /// 按照TaskId查询曲线
        /// </summary>
        /// <returns></returns>
        public static CurveModel GetLastCurve()
        {
            string strMySql = $"select * from curve  order by Id desc limit 1";
            DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
            var listCurve = DbExtension.DataTableToList<CurveModel>(dt);
            return listCurve.Count > 0 ? listCurve[0] : null;
        }

        ///// <summary>
        ///// 添加DDK曲线数据
        ///// </summary>
        ///// <returns></returns>
        //public static bool AddDDKCurve(DDKCurveModel curveModel)
        //{
        //    string strMySql = $"INSERT INTO ddkcurve (TaskId, AngleStep,TimeStep, AngleTorques,TimeTorques) VALUES({curveModel.TaskId}, {curveModel.AngleStep}, {curveModel.TimeStep}, '{curveModel.AngleTorques}', '{curveModel.TimeTorques}'); ";
        //    int result = DalMySQL.ExecuteNonQuery(strMySql);
        //    return result > 0;
        //}

        /// <summary>
        /// 添加DDK曲线数据
        /// </summary>
        /// <returns></returns>
        public static bool AddDDKCurve(DDKCurveModel curveModel)
        {
            //string strMySql = $"INSERT INTO ddkcurve (TaskId, AngleStep,TimeStep, AngleTorques,TimeTorques) VALUES({curveModel.TaskId}, {curveModel.AngleStep}, {curveModel.TimeStep}, '{curveModel.AngleTorques}', '{curveModel.TimeTorques}'); ";
            //mod by ming 2024-11-25
            string strMySql = $@"INSERT INTO ddkcurve (TaskId, AngleStep,TimeStep, AngleTorques,TimeTorques,ProductSN) VALUES(
                                    {curveModel.TaskId}, 
                                    {curveModel.AngleStep}, 
                                    {curveModel.TimeStep}, 
                                    '{curveModel.AngleTorques}', 
                                    '{curveModel.TimeTorques}',
                                    '{curveModel.ProductSN}'
                                ); ";
            int result = DalMySQL.ExecuteNonQuery(strMySql);
            return result > 0;
        }

        ///// <summary>
        ///// 按照TaskId查询DDK曲线
        ///// </summary>
        ///// <returns></returns>
        //public static DDKCurveModel GetDDKCurveByTaskID(long TaskID)
        //{
        //    string strMySql = $"select * from ddkcurve where TaskId={TaskID} ";
        //    DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
        //    var listCurve = DbExtension.DataTableToList<DDKCurveModel>(dt);
        //    if (listCurve.Count > 0)
        //    {
        //        return listCurve[0];
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}

        /// <summary>
        /// 按照TaskId查询DDK曲线
        /// </summary>
        /// <returns></returns>
        public static List<DDKCurveModel> GetDDKCurveByTaskID(long taskID, string productSN)
        {
            //mod by ming 11-25 
            string strMySql = $"select * from ddkcurve where TaskId={taskID} and ProductSN='{productSN}' ";
            DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
            var listCurve = DbExtension.DataTableToList<DDKCurveModel>(dt);
            return listCurve.Count > 0 ? listCurve : null;
        }


        ///// <summary>
        ///// 按照TaskId查询DDK曲线
        ///// </summary>
        ///// <returns></returns>
        //public static DDKCurveModel GetLastDDKCurve()
        //{
        //    string strMySql = $"select * from ddkcurve  order by Id desc limit 1";
        //    DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
        //    var listCurve = DbExtension.DataTableToList<DDKCurveModel>(dt);
        //    if (listCurve.Count > 0)
        //    {
        //        return listCurve[0];
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}

        /// <summary>
        /// 按照TaskId查询DDK曲线 mod by zhu
        /// </summary>
        /// <returns></returns>
        public static List<DDKCurveModel> GetLastDDKCurve()
        {
            string strMySql = $"select * from ddkcurve order by CreateTime desc,Id asc limit 2";
            DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
            var listCurve = DbExtension.DataTableToList<DDKCurveModel>(dt);
            return listCurve.Count > 0 ? listCurve : null;
        }
    }
}
